var constract = require('../../../utils/constract.js');
var app = getApp();
Page({
  data: {},
  onLoad: function(options){
    if (options.state == 0) {
      wx.showModal({
        title: '您还没有名片',
        content: '创建自己的名片，让大家认识你',
        success: function (res) {
          if (res.confirm) {
            wx.redirectTo({
              url: '/pages/add-card/add-card?avatar=' + options.avatar + '&name=' + options.name
            })
          } else if (res.cancel) {
            wx.navigateBack();
          }
        }
      });
      return
    };
  },
  onShow: function () {
    this.showList()
  },
  showList: function(){
    var that = this;
    app.Verification(function (token) {
      wx.request({
        url: constract.invoiceList,
        header: { token: token },
        success: function (res) {
          that.setData({
            list: res.data.data
          })
        }
      })
    })
  },
  deleteItem: function(e){
    var that = this;
    var list = that.data.list;
    var index = that.data.currentIndex;
    var id = e.currentTarget.dataset.id;
    app.Verification(function (token) {
      wx.showModal({
        title: '提示',
        content: '确定删除此发票?',
        success: function (res) {
          if (res.confirm) {
            wx.request({
              url: constract.invoiceDelete,
              header: { token: token },
              data: { id: id },
              success: function (res) {
                that.showList()
              }
            })
          } else if (res.cancel) {
            list[index].txtStyle = '0rpx';
            that.setData({
              list: list
            })
          }
        }
      })
    })
  },
  touchstart:function(e){
    var list = this.data.list;
    var index = e.currentTarget.dataset.index;
    if(e.touches.length==1){
      for (var i = 0; i < list.length; i++) {
        if (i != index) {
          list[i].txtStyle = "right:0rpx";
        }
      };
      this.setData({
        startX: e.touches[0].clientX,
        list:list,
        currentIndex: index
      })
    }
  },
  touchmove: function (e) {
    var that=this;
    if(e.touches.length==1){
      var moveX=e.touches[0].clientX;
      var disX=that.data.startX - moveX;
      var txtStyle='';
      if(disX==0||disX<0){
        txtStyle="left:0px";
      } else if (disX > 0 && disX < 30){
        txtStyle = "left:-" + disX + "px";
      }else{
        txtStyle = "left:-120rpx";
      }
      var index = e.currentTarget.dataset.index;
      var list=this.data.list;
      list[index].txtStyle=txtStyle;
      this.setData({
        list:list
      })
    };
  },
  touchend: function (e) {
    if(e.changedTouches.length==1){
      var endX=e.changedTouches[0].clientX;
      var disX=this.data.startX-endX;
      var txtStyle = disX > 30 ? "left:-120rpx" : "left:0px";
      var index = e.currentTarget.dataset.index;
      var list = this.data.list;
      list[index].txtStyle = txtStyle;
      this.setData({
        list: list
      }) 
    }
  }
})